#!/usr/bin/env python
#-*- coding:utf-8 -*-

class Node(object):
    def __init__(self, nid=None, pos=None, succ=None, prec=None):  #
        self.nid = nid
        self.pos = pos    #position for draw and show
        self.succ = succ  # list of successor nid
        self.prec = prec  # not necessary all the time

    def __eq__(self, other):
        if isinstance(other, Node):
            return False
        return self.nid == other.nid
    
    def __str__(self):
        _string = {'nid': self.nid, 'pos': self.pos, 'succ': self.succ}
        return str(_string)
    
    def __repr__(self):
        _string = {'nid': self.nid, 'pos': self.pos, 'succ': self.succ}
        return str(_string)

    @property
    def succ(self):
        return self._succ

    @succ.setter
    def succ(self, succ):
        if succ is None:
            self._succ = []
        else:
            self._succ = succ

    @property
    def came(self):
        return self._came

    @came.setter
    def came(self, came):
       self._came = came

    @property
    def pos(self):
        return self._pos

    @pos.setter
    def pos(self, pos):
        if pos is None:
            self._pos = (0, 0)
        else:
            self._pos = pos

    @property
    def leaf(self):
        if self.succ:
            return False
        return True

    def add_succ(self, nid):
        if nid not in self.succ:
            self.succ.append(nid)

    def add_prec(self, nid):
        if nid not in self.prec:
            self.prec.append(nid)

    def get_prec(self, idx):
        if not self.prec:
            return None
        if idx < 0 and abs(idx) > len(self.prec):
            return None
        if idx >= 0 and idx >= len(self.prec):
            return None
        return self.prec[idx]

class NodeSet(dict):
    def __call__(self, key):
        if key in self:
            return self[key]
        else:
            print("cannot find key {}".format(key))
            return None

    def nodes(self):
        return self.values()

    def nids(self):
        return self.keys()

    def reset(self):
        self.clear()

    def add_node(self, node):
        if node is None:
            return None
        self[node.nid] = node

    def check_node(self, nid):
        if nid in self:
            return True
        return False

    def add_succ(self, nid, succ):
        if self.check_node(nid):
            self[nid].add_succ(succ)
            return True
        raise Exception("97 {} {}".format(nid, succ))
        return False

    def add_prec(self, nid, succ):
        if self.check_node(nid):
            self[nid].add_prec(succ)
            return True
        raise Exception("97 {} {}".format(nid, prec))
        return False

    def position(self, nid):
        if nid in self:
            return self[nid].pos
        return (0, 0)

    def succ(self, nid):
        if nid in self:
            return self[nid].succ
        return []

    def prec(self, nid):
        if nid in self:
            return self[nid].prec
        return []

if __name__ == "__main__":
    pass